home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Commun⁄Network / RevRdist Folder / RevRdist / RevRdist src / idle.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-21  |  5.6 KB  |  283 lines  |  [TEXT/KAHL]

  1. /*
  2.  * idle.c - the idle process
  3.  *    This is somewhat of a misnomer, since idle() is responsible for
  4.  *    starting the ball rolling and stopping it later.
  5.  */
  6.  
  7. #include "RevRdist.h"
  8. #include "dispatch.h"
  9. #include "TransSkelProto.h"
  10. #include "TransDisplayProto.h"
  11. #include "desktop.h"
  12. #include "volops.h"
  13.  
  14. void                quitOthers(void);
  15. extern StringPtr    syserrlist (OSErr);
  16. static void            touch_prefs(void);
  17.  
  18.  
  19.  
  20. /*
  21.  *=========================================================================
  22.  * idle - the idle process.
  23.  *        this routine is the bottom routine on the dispatch stack and is
  24.  *        the one which starts things going and shuts them down.
  25.  *=========================================================================
  26.  */
  27.  
  28. DISPATCHED (idle)
  29. {
  30.     struct    lm                        /* local memory */
  31.     {
  32.         frame_t            f;
  33.         dnode_t *        dt;            /* control file tree */
  34.     };
  35.     typedef struct lm    lm_t;
  36.     register lm_t *        m;            /* pointer to local memory */
  37.     OSErr                error;
  38.     short                result;
  39.     Boolean                injunk;        /* for matchFolder call */
  40.     Ptr                    paramv[5];    /* param list for pushCalls */
  41.  
  42.     error = 0;
  43.     result = request;
  44.     m = *(lm_t **)fh;
  45.     switch (request)
  46.     {
  47.     case R_INIT:
  48.     /*
  49.      * Initial call.  Just extend frame by needed local memory.
  50.      */
  51.         ErrorMsgs = 0;
  52.         if (error = resizeFrame (fh, sizeof (lm_t)))
  53.             break;
  54.         m = *(lm_t **)fh;
  55.         m->f.state = 1;
  56.         result = R_CONT;
  57.         break;
  58.  
  59.     case R_CONT:
  60.     /*
  61.      * Continue call.  Advance from phase to phase of processing on the
  62.      * control file.
  63.      */
  64.         if (Quit && (Flags & PF_STARTUP) == 0)
  65.         {
  66.             /*
  67.              * Possibly save preference file before quitting
  68.              */
  69.             if (PrefDialog)
  70.                 prefDoFMenu (FILE_CLOSE);
  71.         }
  72.         if (Quit)
  73.             if (m->f.state == 1)
  74.                 return R_QUIT;
  75.             else
  76.                 return R_BACKOUT;
  77.         switch (m->f.state)
  78.         {
  79.         /*
  80.          * Normal state when waiting for user to decide what to do.
  81.          * I.e. to open a control file.
  82.          */
  83.         case 1:
  84.             if (Pending == PA_NULL)
  85.             {
  86.                 /*
  87.                  * Nothing requested, possibly update status display,
  88.                  * but otherwise, do nothing.
  89.                  */
  90.                 if (Pause != S_IDLE)
  91.                 {
  92.                     Pause = S_IDLE;
  93.                     setStat ();
  94.                 }
  95.             }
  96.             if (Pending == PA_PREF)
  97.             {
  98.                 Pending = PA_NULL;
  99.                 doPref ();
  100.             }
  101.             if (Pending != PA_GO)
  102.                 break;
  103.  
  104.             /*
  105.              * Check if everything set up, then go to it!
  106.              * Start by parsing the configured distribution control file
  107.              */
  108.             Pending = PA_NULL;
  109.             if (error = getSet ())
  110.             {
  111.                 if (!(Flags & PF_NOMOUNT))
  112.                     (void) unmount_mounted (0);
  113.                 error = 0;
  114.                 if ((Flags & (PF_STARTUP|PF_LOCKED)) == (PF_STARTUP|PF_LOCKED))
  115.                 {
  116.                     /*
  117.                      * Quit if errors when startup application
  118.                      */
  119.                     Quit = true;
  120.                     result = R_BACKOUT;
  121.                     break;
  122.                 }
  123.                 Quit = false;
  124.                 Pending = PA_PREF;
  125.                 break;
  126.             }
  127.             if (Quit)
  128.                 break;
  129.             Depth = 0;
  130.             m->f.state = 2;
  131.             Watch = GetCursor (watchCursor);
  132.             Pause = S_RUNNING;
  133.             setStat ();
  134.             if (Flags & PF_QUITOTHERS)        /* try again to quit others */
  135.                 quitOthers();
  136.             result = pushCall (parseDistFile, nil);
  137.             break;
  138.  
  139.         case 2:
  140.         /*
  141.          * Check result of parseDistFile
  142.          */
  143.             if (argv == nil || argv[0] == nil)
  144.             {
  145.                 if (ClueID > 0)
  146.                     panic (false, ClueID, nil);
  147.                 else
  148.                     panic (false, E_SYS, nil);
  149.                 return R_BACKOUT;        /* failed */
  150.             }
  151.             m->dt = (dnode_t *) argv[0];
  152.             m->f.state = 3;
  153.             break;
  154.  
  155.         case 3:
  156.         /*
  157.          * Control file parsed okay.  Empty the Junk folder in preparation
  158.          * for moving other files there.
  159.          */
  160.             m->f.state = 4;
  161.             result = pushCall (cleanJunk, nil);
  162.             break;
  163.  
  164.         case 4:
  165.             m->f.state = 5;
  166.             break;
  167.  
  168.         case 5:
  169.         /*
  170.          * Set up to match the root folder against the master
  171.          */
  172.             statMsg ("\p");        /* clear status */
  173.             injunk = false;
  174.             paramv[0] = (Ptr) m->dt;
  175.             paramv[1] = (Ptr) &m->dt->childp->actions;
  176.             paramv[2] = (Ptr) &ServerRoot;
  177.             paramv[3] = (Ptr) &ClientRoot;
  178.             paramv[4] = (Ptr) &injunk;
  179.             m->f.state = 6;
  180.             result = pushCall (matchFolder, paramv);
  181.             break;
  182.  
  183.         case 6:
  184.             updateRoot (m->dt);
  185.             verifyBlessed ();
  186.             result = R_BACKOUT;
  187.             break;
  188.         }
  189.         /*
  190.          * End of R_CONT state switch
  191.          */
  192.         if (error)
  193.         {
  194.             if (error < 0)
  195.             {
  196.                 ClueID = error;
  197.                 error = E_SYS;
  198.             }
  199.             panic (false, error, nil);
  200.             result = R_BACKOUT;
  201.         }
  202.         break;
  203.  
  204.     case R_BACKOUT:
  205.         (void) unmount_mounted ((Flags & PF_UNMOUNTS) ? ServerVol : 0);
  206.         /*
  207.          * Update the prefs mod time
  208.          * a) if there were no errors or
  209.          * b) if we might get in a loop if we don't update it
  210.          */
  211.         if (((Flags & (PF_TOUCH|PF_LISTONLY)) == PF_TOUCH)
  212.         &&    (!Quit
  213.             || (Flags & (PF_STARTUP|PF_RESTART)) == (PF_STARTUP|PF_RESTART)
  214.             )
  215.         )
  216.             touch_prefs();
  217.         /*
  218.          * When we back out to the idle level, wait for something to do
  219.          */
  220.         notice (L_DONE, nil);
  221.         desktop_end();
  222.         if (!(Flags & PF_STARTUP))
  223.         {
  224.             SysBeep (1);
  225.             SysBeep (1);
  226.         }
  227.         result = R_CONT;
  228.         if (ErrorMsgs)
  229.             doWindowMenu (WIND_ERRORS);
  230.         if (SizeResource ((Handle)fh) >= sizeof (lm_t) && m->f.state > 2)
  231.         {
  232.             freeDist (m->dt);
  233.         }
  234.         Quit = false;
  235.         m->f.state = 1;
  236.         if (Flags & PF_STARTUP)
  237.             result = R_QUIT;
  238.         break;
  239.  
  240.     case R_QUIT:
  241.         Quit = true;
  242.         if (!(Flags & PF_NOMOUNT))
  243.         {
  244.             error = remount_vols();
  245.             if (error)
  246.                 warning (E_REMOUNT, syserrlist(error), nil);
  247.         }
  248.         SkelWhoa ();
  249.         SkelBackground (nil);
  250.         break;
  251.     }
  252.     return result;
  253. }
  254.  
  255.  
  256.  
  257. /*
  258.  *=========================================================================
  259.  * touch_prefs() - touch the modification time on the prefs file, if present
  260.  * entry:    File_list[FL_PREF] exists
  261.  *=========================================================================
  262.  */
  263. static
  264. void
  265. touch_prefs(void)
  266. {
  267.     OSErr            error;
  268.     file_info_t        *fi;
  269.  
  270.     if (!File_list)
  271.         return;
  272.     fi = &File_list[FL_PREF];
  273.     if (fi->f_set)
  274.     {
  275.         error = touch (fi->f_vol, &fi->f_info);
  276.         if (error && error != fnfErr)
  277.         {
  278.             ClueID = error;
  279.             panic(true, E_SYS, "\ptouch", Clue1, nil);
  280.         }
  281.     }
  282. }
  283.